<?php

namespace app\dao;

use app\model\TrainingTask;
use app\model\TrainingLogModel;
use think\facade\Log;

class TrainingDao
{
    // 创建训练任务
    public static function createTask($projectId, $title, $content, $startTime, $endTime, $creatorId)
    {
        try {
            return TrainingTask::create([
                'project_id' => $projectId,
                'title' => $title,
                'content' => $content,
                'start_time' => $startTime,
                'end_time' => $endTime,
                'creator_id' => $creatorId
            ]);
        } catch (\Exception $e) {
            Log::error("创建训练任务失败: " . $e->getMessage());
            return null;
        }
    }

    // 根据任务 ID 获取训练任务
    public static function getTaskById($taskId)
    {
        try {
            return TrainingTask::find($taskId);
        } catch (\Exception $e) {
            Log::error("根据任务ID获取训练任务失败: " . $e->getMessage());
            return null;
        }
    }

    // 获取项目下的所有训练任务
    public static function getTasksByProjectId($projectId)
    {
        try {
            return TrainingTask::where('project_id', $projectId)->select();
        } catch (\Exception $e) {
            Log::error("根据项目ID获取训练任务失败: " . $e->getMessage());
            return [];
        }
    }

    // 提交训练日志
    public static function submitLog($memberId, $taskId, $logContent)
    {
        try {
            return TrainingLogModel::create([
                'member_id' => $memberId,
                'task_id' => $taskId,
                'log_content' => $logContent
            ]);
        } catch (\Exception $e) {
            Log::error("提交训练日志失败: " . $e->getMessage());
            return null;
        }
    }

    // 获取成员的训练日志
    public static function getLogsByMemberId($memberId)
    {
        try {
            return TrainingLogModel::where('member_id', $memberId)->select();
        } catch (\Exception $e) {
            Log::error("根据成员ID获取训练日志失败: " . $e->getMessage());
            return [];
        }
    }
}
